using System;
using System.Collections.Generic;
using System.Text;

   public class Typecast
   {
      public Typecast() { }
      public static byte ToByte(object x)
      {
         try
         {
            return Convert.ToByte(x);
         }
         catch (Exception) { }
         return 0;
      }
      public static Int16 ToInt16(object x)
      {
         try
         {
            return Convert.ToInt16(x);
         }
         catch (Exception) { }
         return 0;
      }

      public static Int16 ToSmallIDType(object x)
      {
         return ToInt16(x);
      }
      public static DateTime DefaultDateTime
      {
         get
         {
            return new DateTime(1, 1, 1);
         }
      }

      public static DateTime MaximumDateTime
      {
         get
         {
            return new DateTime(2075, 12, 31);
         }
      }

      public static char ToChar(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToChar(x);
         }
         catch (Exception) { return (char)0; }
      }

      public static string ToString(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToString(x);
         }
         catch (Exception) { return ""; }
      }

      public static int ToInt32(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToInt32(x);
         }
         catch (Exception) { return 0; }
      }
      public static long ToInt64(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToInt64(x);
         }
         catch (Exception) { return 0; }
      }
      public static DateTime ToDateTime(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToDateTime(x);
         }
         catch (Exception) { return DefaultDateTime; }
      }

      public static bool ToBoolean(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            if (ToInt32(x) == 1)
               return true;

            return Convert.ToBoolean(x);
         }
         catch (Exception) { return false; }
      }

      public static decimal ToDecimal(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToDecimal(x);
         }
         catch (Exception) { return 0; }
      }

      public static Double ToDouble(object x)
      {
         try
         {
            if (x == null) throw new NullReferenceException("x is null");

            return Convert.ToDouble(x);
         }
         catch (Exception) { return 0; }
      }

      public static Guid ToGuid(string x)
      {
         try
         {
            return new Guid(x);
         }
         catch (Exception)
         {
            return Guid.Empty;
         }
      }

      public static Guid ToGuid(object x)
      {
         try
         {
            Guid result = (Guid)x;

            if (result != Guid.Empty)
               return result;

            throw new Exception();
         }
         catch (Exception)
         {
            try
            {
               return ToGuid(x.ToString());
            }
            catch
            {
               return Guid.Empty;
            }
         }
      }
   }
